package com.sfeir.tv.client.ui.search;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.sfeir.tv.client.mvc.framework.AppEvent;
import com.sfeir.tv.client.mvc.framework.Model;
import com.sfeir.tv.client.mvc.framework.View;
import com.sfeir.tv.client.mvc.framework.impl.gwt.GWTAbsolutePanel;

public class ItemView extends GWTAbsolutePanel {
	private Image image = new Image();
	private Label titleLabel = new Label();
	private HTML description = new HTML();

	public ItemView() {
		this("item-" + System.currentTimeMillis());
	}

	public ItemView(String id) {
		super(id);
		image.setSize("75px", "75px");
		add(image, 0, 0);
		add(titleLabel, 79, 0);
		description.setSize("400px", "45px");
		add(description, 89, 23);
	}
	
	public void refreshView(Model model) {
		image.setUrl(((ItemModel)model).getImageURL());
		titleLabel.setText(((ItemModel)model).getTitle());
		description.setHTML(((ItemModel)model).getDescription());
	}

	public void fireEvent(int eventType) {
        controller.fireEvent( new AppEvent(eventType, controller.getModel() ) );
	}
	
	
    public void onGWTBrowserEvent( Event event )
    {
        if (DOM.eventGetType(event) == Event.ONMOUSEDOWN) {
            fireEvent( AppEvent.CLICK_EVENT );
        } 
    }

    // ======================================================================
    public void dispose()
    {
        // TODO Auto-generated method stub
        
    }

    public boolean isDisposed()
    {
        // TODO Auto-generated method stub
        return false;
    }

    public void doInit()
    {
        // TODO Auto-generated method stub
        
    }

    public void init()
    {
        // TODO Auto-generated method stub
        pre();
        doInit();
        post();
    }

    public void post()
    {
        // should find another solution to this
        refreshView( controller.getModel() );
//        super.post();
    }

    public void pre()
    {
        // TODO Auto-generated method stub
        
    }

}
